package com.junzhiit.nengji.cms.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.cms.service.SellSetingService;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SellSetingVO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.SellSetingDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.model.SellSeting;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class SellSetingServiceImpl extends BaseServiceImpl implements SellSetingService {

	@Autowired
	private SellSetingDAO sellSetingDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public ResultVO find() {
		ResultVO result = new ResultVO();
		SellSeting sellSeting = sellSetingDAO.find(Constants.SELL_SETING_KEY);
		result.setData(sellSeting);
		return result;
	}

	@Override
	@Transactional
	public ResultVO update(SellSetingVO sellSetingVO){
		ResultVO result=new ResultVO();
		SellSeting sellSeting=new SellSeting();
		BeanUtils.copyProperties(sellSetingVO, sellSeting);
		//如果关闭分销,把所有商品及商家都设置不开启分销
		if(sellSetingVO.getIsOpenSell() == Is.NO.toIntValue()){
			goodsDAO.updateIsOpenSell();
			shopDAO.updateIsOpenSell();
		}
		sellSetingDAO.update(sellSeting);
		result.setData(sellSeting);
		return result;
	}
	
	public SellSeting findSellSeting(){
		return sellSetingDAO.find(Constants.SELL_SETING_KEY);
	}
	
}
